package com.yoda.mall.admin.controller;

import com.yoda.common.exception.BusinessException;
import com.yoda.common.exception.ErrorResult;
import com.yoda.mall.admin.service.OrderService;
import com.yoda.mall.model.entity.oms.Order;
import com.yoda.mall.model.vo.DeliveryParamVo;
import com.yoda.mall.model.vo.OrderDetailVo;
import com.yoda.mall.model.vo.PageResult;
import com.yoda.mall.model.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author wjy
 * @date 2022年 12月 18日 15:32
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;


    @PostMapping("/update/receiverInfo")
    public R updateReceiverInfo(@RequestBody Order receiverInfoParam){
        Integer count = orderService.updateOrderById(receiverInfoParam);
        return R.success(count);
    }
    @PostMapping("update/note")
    public R noteOrder(Long id,String note,Integer status){
        Order order = new Order();
        order.setId(id);
        order.setNote(note);
        order.setStatus(status);
        Integer count = orderService.updateOrderById(order);
        return R.success(count);
    }

    /**
     * 通过id获取订单详细信息
     *
     * @param id id
     * @return {@link R}
     */
    @GetMapping("/{id}")
    public R getOrderDetailById(@PathVariable Long id){
        OrderDetailVo vo = orderService.getOrderDetailById(id);
        return R.success(vo);
    }

    /**
     * 批量删除兼单个删除
     *
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public R orderDle(@RequestParam("ids") List<Integer> ids) {
        orderService.orderDle(ids);
        return R.success("删除成功");
    }

    /**
     * 查询订单
     * @param pageNum
     * @param pageSize
     * @param createTime
     * @param orderSn
     * @param orderType
     * @param receiverKeyword
     * @param sourceType
     * @param status
     * @return
     */
    @GetMapping("/list")
    public R orderList(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String createTime, String orderSn,
                       Integer orderType, String receiverKeyword
            , Integer sourceType, Integer status) throws ParseException {
        PageResult<Order> orderPage = orderService.orderList(pageNum,pageSize,createTime,orderType
                ,orderSn,sourceType,status,receiverKeyword);
        return R.success(orderPage);


    }

    /**
     * 批量关闭订单
     * @param ids
     * @param note 订单备注
     * @return
     */
    @PostMapping("/update/close")
    public R updateClose(@RequestParam(defaultValue = "ids") List<Long> ids,String note){
        orderService.updateClose(ids, note);
        return R.success("修改成功");
    }

    /**
     * 批量发货

     */
    @PostMapping("/update/delivery")
    public R updateDelivery(@RequestBody List<DeliveryParamVo> deliveryParamList){
        List<Order> orders = deliveryParamList.stream().map(DeliveryParamVo::init).collect(Collectors.toList());
        Integer count =  orderService.updateDelivery(orders);
        if (count==0){
            throw new BusinessException(ErrorResult.builder().errMessage("此操作仅对待发货订单生效!").errCode(10000).build());
        }
        return R.success(count);
    }

}
